package com.xiaoxu.media.controller;


import com.xiaoxu.base.exception.bkSchoolException;
import com.xiaoxu.base.model.PageParams;
import com.xiaoxu.base.model.PageResult;
import com.xiaoxu.base.model.RestResponse;
import com.xiaoxu.media.model.dto.QueryMediaParamsDto;
import com.xiaoxu.media.model.dto.UploadFileParamsDto;
import com.xiaoxu.media.model.dto.UploadResultMediaDto;
import com.xiaoxu.media.model.po.MediaFiles;
import com.xiaoxu.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author xiaoxu
 * @version 1.0
 * @description 媒资文件管理接口
 * @date 2023/1/20 15:29
 */
@Api(value = "媒资文件管理接口", tags = "媒资文件管理接口")
@RestController
public class MediaFilesController {


    final
    MediaFileService mediaFileService;

    public MediaFilesController(MediaFileService mediaFileService) {
        this.mediaFileService = mediaFileService;
    }


    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto) {
        Long companyId = 1232141425L;
        return mediaFileService.queryMediaFiels(companyId, pageParams, queryMediaParamsDto);
    }

    @ApiOperation("上传文件")
    @RequestMapping(value = "/upload/coursefile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public UploadResultMediaDto upload(
            @RequestPart("filedata") MultipartFile filedata,
            @RequestParam(value = "folder", required = false) String folder,
            @RequestParam(value = "objectName", required = false) String objectName
    ) {
        //机构Id
        Long companyId = 1232141425L;
        String contentType = filedata.getContentType();
        UploadFileParamsDto params = new UploadFileParamsDto();
        //上传类型
        params.setContentType(contentType);
        //文件大小
        params.setFileSize(filedata.getSize());
        //文件类型
        assert contentType != null;
        if(contentType.contains("image")){
            //图片
            params.setFileType("001001");
        }else{
            //其它
            params.setFileType("001003");
        }
        //文件名称
        params.setFilename(filedata.getOriginalFilename());
        UploadResultMediaDto resultMediaDto;
        try {
             resultMediaDto = mediaFileService.uploadFile(companyId, filedata.getBytes(), params, folder, objectName);
        } catch (IOException e) {
            throw new bkSchoolException(e.getMessage());
        }
        return resultMediaDto;
    }

    @ApiOperation("预览文件")
    @GetMapping("/preview/{mediaId}")
    public RestResponse<String> getPlayUrlByMediaId(@PathVariable Long mediaId){
        if (mediaId == null){
            throw new bkSchoolException("错误，参数非法");
        }
        MediaFiles file = mediaFileService.getFileById(mediaId);
        if (file == null){
            throw new bkSchoolException("业务异常，文件找不到");
        }
        return RestResponse.success(file.getUrl());
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/{mediaId}")
    public void deleteMediaFileByMediaId(@PathVariable Long mediaId){
        if (mediaId == null){
            throw new bkSchoolException("错误，参数非法");
        }
        mediaFileService.deleteDBFile(mediaId);
    }



}
